package com.ky.circle.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.ky.circle.entity.CircleArticle;
import com.ky.circle.mapper.ArticleMapper;
import com.ky.circle.mapper.ListMapper;
import com.ky.common.config.SystemConfig;
import com.ky.common.param.CircleArticleAdd;
import com.ky.common.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author lcr
 * @since 2024-06-17
 */
@RestController
@RequestMapping("/circle")
public class CircleArticleController {

    @Autowired
    private ArticleMapper articleMapper;



    @RequestMapping("/selectAll")
    public R selectArticleList(){
        List<CircleArticle> list = articleMapper.selectList(null);
        return R.ok(list);

    }


    @RequestMapping("/selectTypeId")
    public R selectTypeId(){
        QueryWrapper<CircleArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_id",1);
        List<CircleArticle> list = articleMapper.selectList(queryWrapper);
        return R.ok(list);
    }

    @RequestMapping("/addCircleArticle")
    public R addCircleArticle(HttpServletRequest request, @RequestBody CircleArticleAdd circleArticleAdd){

        try{
            //通过token获取用户id
            String token = request.getHeader(SystemConfig.HEADER_TOKEN);
            if(token == null || token.equals("")){
                return R.failed("token不能为空");
            }
            Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();


            //创建帖子对象
            CircleArticle circleArticle = new CircleArticle();
            circleArticle.setUserId(id);
            circleArticle.setContent(circleArticleAdd.getContent());
            circleArticle.setTypeId(Integer.parseInt(circleArticleAdd.getTypeId()));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            circleArticle.setCreateTime(simpleDateFormat.format(new Date()));
            for (String image : circleArticleAdd.getUrlList()) {
                circleArticle.setContentImg(image);
            }
            articleMapper.insert(circleArticle);
        } catch (Exception e) {
            return R.failed("添加失败");
        }
        return R.ok("添加成功");
    }

    @RequestMapping("/selectById/{id}")
    public R selectById(@PathVariable Integer id){
        QueryWrapper<CircleArticle> circleArticleQueryWrapper = new QueryWrapper<>();
        circleArticleQueryWrapper.eq("id",id);
        CircleArticle circleArticle = articleMapper.selectOne(circleArticleQueryWrapper);
        return R.ok(circleArticle);
    }



    @RequestMapping("/addPraises/{id}")
    public R addPraises(@PathVariable int id){
        QueryWrapper<CircleArticle> circleArticleQueryWrapper = new QueryWrapper<>();
        circleArticleQueryWrapper.eq("id",id);
        CircleArticle circleArticle = articleMapper.selectOne(circleArticleQueryWrapper);
        circleArticle.setPraises(circleArticle.getPraises()+1);
        articleMapper.update(circleArticle,circleArticleQueryWrapper);
        return R.ok(circleArticle);

    }


    @RequestMapping("/addCollect/{id}")
    public R addCollect(@PathVariable int id){
        QueryWrapper<CircleArticle> circleArticleQueryWrapper = new QueryWrapper<>();
        circleArticleQueryWrapper.eq("id",id);
        CircleArticle circleArticle = articleMapper.selectOne(circleArticleQueryWrapper);
        circleArticle.setCollect(circleArticle.getCollect()+1);
        articleMapper.update(circleArticle,circleArticleQueryWrapper);
        return R.ok(circleArticle);

    }

    @RequestMapping("/addReview")
    public R addReview(int id){
        QueryWrapper<CircleArticle> circleArticleQueryWrapper = new QueryWrapper<>();
        circleArticleQueryWrapper.eq("id",id);
        CircleArticle circleArticle = articleMapper.selectOne(circleArticleQueryWrapper);
        circleArticle.setReview(circleArticle.getReview()+1);
        articleMapper.update(circleArticle,circleArticleQueryWrapper);
        return R.ok(circleArticle);

    }

    @RequestMapping("/praises/{id}/praises")
    public R praises(@PathVariable int id){
        QueryWrapper<CircleArticle> circleArticleQueryWrapper = new QueryWrapper<>();
        circleArticleQueryWrapper.eq("id",id);
        CircleArticle circleArticle = articleMapper.selectOne(circleArticleQueryWrapper);
        return R.ok(circleArticle.getPraises());
    }


    @RequestMapping("/collect/{id}/collect")
    public R collect(@PathVariable int id){
        QueryWrapper<CircleArticle> circleArticleQueryWrapper = new QueryWrapper<>();
        circleArticleQueryWrapper.eq("id",id);
        CircleArticle circleArticle = articleMapper.selectOne(circleArticleQueryWrapper);
        return R.ok(circleArticle.getCollect());
    }

}

